package com.etc.service.impl;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;
import javax.swing.text.html.HTMLDocument.HTMLReader.HiddenAction;

import org.springframework.stereotype.Service;

import com.etc.dao.CityMapper;
import com.etc.dao.ProvinceMapper;
import com.etc.dao.ScenicMapper;
import com.etc.dao.SdetailsMapper;
import com.etc.dao.SdphotoMapper;
import com.etc.entity.Province;
import com.etc.entity.Scenic;
import com.etc.entity.ScenicMessage;
import com.etc.entity.SdetailsWithBLOBs;
import com.etc.entity.Sdphoto;
import com.etc.entity.User;
import com.etc.service.ScenicService;
import com.etc.utils.PageData;

@Service
public class ScenicServiceImpl implements ScenicService {
	@Resource
	private ScenicMapper scmapper; // 景点dao层接口
	@Resource
	private CityMapper cmapper; // 城市dao层接口
	@Resource
	private ProvinceMapper promepper; // 省dao层接口
	@Resource
	private SdphotoMapper sdpmapper; // 景点dao层接口
	@Resource
	private ScenicMapper scenicmapper;
	@Resource
	private SdetailsMapper sdetmapper;//景点具体信息dao层接口

	// 根据省份或城市查询该地的景点
	@Override
	public PageData<ScenicMessage> findScenicBypIdAndcId(int page, int pageSize, int pid, int cid, int hid) {
		PageData<ScenicMessage> pd = new PageData<>();
		List<ScenicMessage> lsm = new ArrayList<ScenicMessage>();
		int star = (page - 1) * pageSize; // 计算起始位置
		int count = scmapper.selectCountPage(pid, cid, hid);// 总长度
		List<Scenic> listScenic = scmapper.selectByPage(star, pageSize, pid, cid, hid);
		for (int i = 0; i < listScenic.size(); i++) {
			Scenic s = listScenic.get(i);
			ScenicMessage sm = new ScenicMessage(s.getSid(), s.getSimg(), s.getSname(), s.getSaddress(), s.getSlong(),
					s.getSlat(), s.getSsatisfaction(), s.getSticket(), s.getStime(), s.getSbt(), s.getHid());
			String pro = promepper.selectByproId(s.getSprovince()).getProvincename();
			String city = "";
			if (s.getScity() != 0) {
				city = cmapper.selectByCityId(s.getScity()).getCityname();
			}
			sm.setSprovince(pro);
			sm.setScity(city);
			List<Sdphoto> listphoto = sdpmapper.selectBySid(s.getSid());
			sm.setBigimg(listphoto);
			lsm.add(sm);
		}
		pd.setData(lsm);
		pd.setCount(count);
		pd.setPage(page);
		pd.setLimit(pageSize);
		pd.setCode(0);
		pd.setMsg("操作成功");

		return pd;
	}

	@Override
	public PageData<Scenic> selectThemeByhid(int page, int pagesize, String content) {
		// TODO Auto-generated method stub
		int start = (page - 1) * pagesize;
		List<Scenic> data = scenicmapper.selectThemeByPage(start, pagesize, content);
		int count = scenicmapper.selectCount(content);
		PageData<Scenic> pd = new PageData<>(data, count, pagesize, page);
		return pd;
	}

	//根据省份、城市查询、及景点主题查询所有景点信息
	@Override
	public List<Scenic> findScenicByPidCidAndHid(int pid, int cid, int hid) {
		
		return scmapper.selectByPCHid(pid, cid, hid);
	}

	//根据省份查询该省热门景点4个按有个按游客满意度降序排列
	@Override
	public List<ScenicMessage> findHotScenicByPname(String pname) {
		
		String prov = pname.substring(0, 2);
		//System.out.println("prov:"+prov);
		Province pro = promepper.selectByPname(prov);
		List<Scenic> listScenic = scmapper.selectHotScenicByPid(pro.getProvinceid());
		List<ScenicMessage> lsm = new ArrayList<ScenicMessage>();
		for (int i = 0; i < listScenic.size(); i++) {
			Scenic s = listScenic.get(i);
			ScenicMessage sm = new ScenicMessage(s.getSid(), s.getSimg(), s.getSname(), s.getSaddress(), s.getSlong(),
					s.getSlat(), s.getSsatisfaction(), s.getSticket(), s.getStime(), s.getSbt(), s.getHid());
			String pro1 = pro.getProvincename();
			String city = "";
			if (s.getScity() != 0) {
				city = cmapper.selectByCityId(s.getScity()).getCityname();
			}
			sm.setSprovince(pro1);
			sm.setScity(city);
			List<Sdphoto> listphoto = sdpmapper.selectBySid(s.getSid());
			sm.setBigimg(listphoto);
			lsm.add(sm);
		}
		return lsm;
	}

	//根据景点的id查询景点的具体详细信息
	@Override
	public SdetailsWithBLOBs findScenicMessageBySid(int sid) {
		
		return sdetmapper.selectBySid(sid);
	}

}
